<template>
  <div>
    <!-- 头部导航标签 -->
    <van-nav-bar fixed>
      <template #left>
        <!-- <van-image :src="require('../../assets/toutiao_logo.png')"></van-image> -->
        <img src="../../assets/toutiao_logo.png" alt="" class="logo" />
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem" color="white" />
      </template>
    </van-nav-bar>
    <!-- 头部内容导航 -->
    <div class="top">
      <van-tabs
        v-model="active"
        @click="tabFn"
        sticky
        offset-top="1.22666667rem"
      >
        <van-tab v-for="obj in list" :key="obj.id" :title="obj.name" :name="obj.id">
          <!-- 内容区域 -->
          <div>
            <van-pull-refresh v-model="isLoading" @refresh="onRefresh"  loading-text="" >
              <van-list v-model="loading"  :finished="finished"  finished-text="没有更多了"  @load="onLoad" >
                <ArticleList :list="articleList" @removeA="removeFn"></ArticleList>
              </van-list>
            </van-pull-refresh>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import { userChannelAPI, getArticleListAPI, dislikeArticleAPI, reportsAPI } from '@/api'
import { Toast } from 'vant'

import ArticleList from './components/ArticleList'
export default {
  data () {
    return {
      active: '',
      list: [],
      articleList: [],
      selChannelId: 0,
      isLoading: false,
      finished: false,
      loading: false,
      page: 1
    }
  },
  components: {
    ArticleList
  },
  async created () {
    const [, res] = await userChannelAPI()
    this.list = res.data.data.channels
    this.getArticleList()
  },
  methods: {
    async getArticleList () {
      const [, ares] = await getArticleListAPI({
        channel_id: this.selChannelId,
        timestamp: new Date().getTime().toString(),
        // timestamp:1556789000001
        with_top: 1,
        page: this.page
      })
      // 判断没有的数据的时候应该怎么弄
      if (ares.data.data?.results.length === 0) {
        this.finished = true
        return
      }
      console.log(this.articleList)
      ares.data.data.results.forEach(obj => this.articleList.push(obj))

      this.isLoading = false
      this.loading = false
      Toast.clear()
    },
    tabFn (nameVal) {
      this.selChannelId = nameVal
      this.articleList = []
      this.page = 1
      this.getArticleList()
    },
    onRefresh () {
      // 下拉刷新
      this.isLoading = false
      this.articleList = []
      this.page = 1
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      this.getArticleList()
    },
    onLoad () {
      this.page++
      this.getArticleList()
    },
    async removeFn (art_id, type) {
      const index = this.articleList.findIndex(obj => obj.art_id === art_id)
      this.articleList.splice(index, 1)
      if (type === undefined) {
        await dislikeArticleAPI({
          target: art_id.toString()

        })
      } else {
        await reportsAPI({
          target: art_id,
          type: type
        })
      }
      this.page = 1
      this.articleList = []
      this.getArticleList()
    }
  }
}
</script>

<style scoped>
.top {
  margin-top: 46px;
}
.logo {
  width: 115px;
  height: 37px;
}
</style>
